package inet.app.view.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;

public class KetQuaDetailAdapter extends FragmentPagerAdapter {
	private Context _context;
	private ArrayList<Fragment> items;
	private ViewPager mContainer;
	private FragmentManager mFragmentManager;

	public KetQuaDetailAdapter(Context context, FragmentManager fm) {
		super(fm);
		this.mFragmentManager = fm;
		_context = context;
	}

	public KetQuaDetailAdapter(Context context, FragmentManager fm,
			ArrayList<Fragment> items) {
		super(fm);
		_context = context;
		this.items = items;
		this.mFragmentManager = fm;
	}

	public KetQuaDetailAdapter(Context context, ViewPager container,
			FragmentManager fm, ArrayList<Fragment> items, String title[]) {
		super(fm);
		_context = context;
		this.items = items;
		this.mContainer = container;
		this.mFragmentManager = fm;
	}
	public KetQuaDetailAdapter(Context context, ViewPager container,
			FragmentManager fm, ArrayList<Fragment> items) {
		super(fm);
		_context = context;
		this.items = items;
		this.mContainer = container;
		this.mFragmentManager = fm;
	}
	@Override
	public Fragment getItem(int position) {
		return items.get(position);
	}

	@Override
	public int getCount() {
		return items.size();
	}

	public CharSequence getPageTitle(int position) {
		return null;
	}
	public void update(Fragment fragment){
		items.add(0,fragment);
		notifyDataSetChanged();
	}
	public void replace(int position, Fragment fragment) {
		// Get currently active fragment.
		Fragment old_fragment = items.get(position);
		if (old_fragment == null) {
			return;
		}
		// Replace the fragment using transaction and in underlaying array list.
		// NOTE .addToBackStack(null) doesn't work
		this.startUpdate(mContainer);
		mFragmentManager.beginTransaction().setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
        	.remove(old_fragment).add(mContainer.getId(), fragment).addToBackStack(null)
        	.commit();
		items.set(position, fragment);
		this.notifyDataSetChanged();
		this.finishUpdate(mContainer);
    }
}
